package com.wfh.adapter;

import java.lang.ref.SoftReference;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.wfh.R;
import com.wfh.bean.ProductBean;
import com.wfh.util.Util;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.PagerAdapter;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

public class ProductAdapter extends PagerAdapter {
	public List<ProductBean> bean;
//	private List<View> viewlist=new ArrayList<View>();
	private Context context;
	private Map<String, SoftReference<Bitmap>> tempcache = new HashMap<String, SoftReference<Bitmap>>();

	public ProductAdapter(Context context, List<ProductBean> bean) {
		this.bean = bean;
		this.context = context;
//		initView();
	}

	@Override
	public void destroyItem(ViewGroup container, int position, Object view) {
		Log.i("位置",position+"");
		container.removeView((View)view);
	}

	@Override
	public Object instantiateItem(ViewGroup container, int i) {
		ImageView imgView = new ImageView(context);
		imgView.setImageResource(R.drawable.nopic);
		
		if (tempcache.containsKey(bean.get(i).getProduct_img()) && tempcache.get(bean.get(i).getProduct_img()).get() != null) {
//			Log.i("我用的是缓存","是的");
			imgView.setImageBitmap(tempcache.get(
					bean.get(i).getProduct_img()).get());
			container.addView(imgView);
			return imgView;
		}

		loadDrawable(bean.get(i).getProduct_img(), imgView,
				new ImageCallback() {

					@Override
					public void imageloaded(Bitmap bitmap,
							ImageView imageview, String imgurl) {
						imageview.setImageBitmap(bitmap);
						ProductAdapter.this.notifyDataSetChanged();
//						Log.i("我是新加载的","是的");
					}
				});
		
		container.addView(imgView);
//		viewlist.add(imgView);
		return imgView;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return bean.size();
	}
	
	

	@Override
	public boolean isViewFromObject(View arg0, Object arg1) {
		// TODO Auto-generated method stub
		return arg0 == arg1;
	}


	public void loadDrawable(final String imageUrl, final ImageView imageView,
			final ImageCallback imageCallback) {

		final Handler handler = new Handler() {
			public void handleMessage(Message message) {
				imageCallback.imageloaded((Bitmap) message.obj, imageView,
						imageUrl);
			}
		};

		new Thread() {
			public void run() {
				Bitmap bitmap = Util.getBitmap(imageUrl,1);
				Log.i("bitmap3","----------"+bitmap.toString());
				tempcache.put(imageUrl, new SoftReference<Bitmap>(bitmap));// 二级缓存
				Message message = handler.obtainMessage(0, bitmap);
				handler.sendMessage(message);
			}
		}.start();
	}

	// imagecallback
	public interface ImageCallback {
		public void imageloaded(Bitmap bitmap, ImageView imageview,
				String imgurl);
	}

	public void clearImgcache() {
		if (tempcache != null && tempcache.size() > 0) {
			Log.i("imagesize", tempcache.size() + "");
			Set<String> keys = tempcache.keySet();
			for (Iterator it = keys.iterator(); it.hasNext();) {
				String s = (String) it.next();
				Bitmap bmp = tempcache.get(s).get();
				if (bmp != null) {
					if (bmp.isRecycled() == false) {
						bmp.recycle();
						System.gc();
					}
				}
			}
			tempcache.clear();
		}
	}

}